package com.synergy.stockflow.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.synergy.stockflow.constant.ResultCode;
import com.synergy.stockflow.model.Result;
import com.synergy.stockflow.product.entity.Material;
import com.synergy.stockflow.product.entity.ProcessStep;
import com.synergy.stockflow.product.entity.UnitConversion;
import com.synergy.stockflow.product.service.ProcessStepService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/processStep")
public class ProcessStepController {
    @Autowired
    private ProcessStepService processStepService;
    @GetMapping("index")
    public Result<Page<ProcessStep>> page(@RequestParam(name = "current", defaultValue = "1") int current,
                                             @RequestParam(name = "size", defaultValue = "5") int size,
                                             @RequestParam(name = "processStepName", defaultValue = "") String processStepName,
                                             @RequestParam(name = "processStepId", defaultValue = "") String processStepId,
                                             @RequestParam(name = "isEnabled", defaultValue = "") String isEnabled,
                                             @RequestParam(name = "materialId", defaultValue = "") String materialId){
        QueryWrapper<ProcessStep> query = new QueryWrapper<>();
        if(processStepName != null && processStepName.length() > 0){
            query.lambda().like(ProcessStep::getProcessName,processStepName);
        }
        if(processStepId != null && processStepId.length() > 0){
            query.lambda().like(ProcessStep::getProcessId,processStepId);
        }
        if(isEnabled != null){
            query.lambda().like(ProcessStep::getIsEnabled,isEnabled);
        }
        query.lambda().orderBy(true,false, ProcessStep::getCreateTime);
        Page<ProcessStep>page=new Page<>(current,size);
        page=processStepService.page(page,query);
        List<ProcessStep> list = processStepService.list();
        return Result.success(page);
    }
        @GetMapping("/Name")
        public Result selectName(){
            return Result.success(processStepService.list());
        }
    @PostMapping("/insert")
    public Result insert(@RequestBody ProcessStep processStep){
        QueryWrapper<ProcessStep> query = new QueryWrapper<>();
        if (processStep.getProcessId() != null && processStep.getProcessId().length() > 0) {
            query.lambda().eq(ProcessStep::getProcessId, processStep.getProcessId());
        }
        Page<ProcessStep> page = new Page<>(1, 10000);
        Page<ProcessStep> page1 = processStepService.page(page, query);
        System.out.println(page1);
        if (page1 == null || page1.getRecords().size() == 0) {

            processStep.setCreateTime(new Date());
            processStep.setUpdateTime(new Date());
            processStepService.save(processStep);
            return Result.success(processStep);
        }
        return Result.error(ResultCode.BUSINESS_EXCEPTION.value(), "工序编码已存在");
    }
    @PutMapping("/update")
    public Result update(@RequestBody ProcessStep processStep){
        processStep.setUpdateTime(new Date());
        processStepService.updateById(processStep);
        return Result.success(processStep);
    }
    @DeleteMapping("/delete")
    public Result delete(@RequestBody List<String> processStepIds){
        processStepService.removeBatchByIds(processStepIds);
        return Result.success(processStepIds);
    }
}
